package com.gdit.zax.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.RemindDao;
import com.gdit.zax.entitys.Remind;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.DateUtil;

public class RemindDaoImpl implements RemindDao {
	private PreparedStatement ps;
	private ResultSet rs;
	
	private Connection conn;
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	
	public RemindDaoImpl(Connection conn,HttpServletRequest request, HttpServletResponse response) {
		this.conn = conn;
		this.request = request;
		this.response = response;
	}
	
	public boolean addRemind(Remind data) {
		try {
			String sql = "insert t_remind set Theme=?,Plan=?,UserId=?,PlanId=?,Time=?,Repear=?,Ring=?,Vibrator=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, data.getTheme());
			ps.setString(2, data.getPlan());
			ps.setInt(3, data.getUserid());
			ps.setInt(4, data.getPlanid());
			try {
				long timemi = DateUtil.getDate(data.getTime(), "HH:mm").getTime();
				ps.setTime(5, new Time(timemi));
//				ps.setTime(5,new Time(Time.parse(data.getTime())));
			} catch (Exception e) {
				e.printStackTrace();
			}
			ps.setString(6, data.getRepear());
			ps.setInt(7, data.getRing());
			ps.setInt(8, data.getVibrator());
			
			int result = ps.executeUpdate();
			if (result!=0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean delRemind(int rid) {
		// TODO Auto-generated method stub
		return false;
	}

	public ArrayList<Remind> listRemid(int page, int onepagecount) {
		ArrayList<Remind> list = new ArrayList<Remind>();
		String sql = "select * from t_remind limit ?,? ";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (page-1)*onepagecount);
			ps.setInt(2, onepagecount);
			
			rs = ps.executeQuery();
			
			while(rs.next()){
				Remind be = DBUtil.getobject(rs, Remind.class);
				list.add(be);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public ArrayList<Remind> listRemid(int uid, int page, int onepagecount) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<Remind> listAllRemid() {
		// TODO Auto-generated method stub
		return null;
	}

}
